﻿var gMaHV =0;
var gMaLop = 0;
$(document).ready(function () {
    $("#themHV").click(function () {
        DanhSachHocVien();
        return false;
    });

    $("#fthemhocvien").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 170,
        minWidth: 200,
        buttons: [
           {
               html: "<span class='glyphicon glyphicon-search'></span> Thêm", class: "btn btn-primary", click: function () {
                   ThemHocVienLop();
               }
           }
          
        ]
    });
    $("#fCapNhat").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 170,
        minWidth: 200,
        buttons: [
           {
               html: "<span class='glyphicon glyphicon-search'></span> Cập nhật", class: "btn btn-primary", click: function () {
                   CapNhatHocVienLop();
               }
           }

        ]
    });
});

function DanhSachHocVien() {
    $.ajax({
        type: "POST",
        url: "../Ajax/HocVienAjax.aspx",
        data: { Methods: "DanhSach" },
        success: function (result) {
            result = $.parseJSON(result);
            var option = "";
            $.each(result, function (i,o) {
                option += "<option value='" + o.MaHV + "'>" + o.MaHV + "-" + o.TenHV + "</option>";
            });
            $("#drlDsHocVien").html(option);
            $("#fthemhocvien").dialog("open");
        },
        error: function () {

        }
    });
}

function ThemHocVienLop() {
    var mahocvien = $("#drlDsHocVien").find("option:selected").val();
    var malop = $("#ddllophoc").val();
    $.ajax({
        type: "POST",
        url: "../Ajax/HocVienAjax.aspx",
        data: { Methods: "ThemHocVienLop", HocVien: mahocvien, Lop: malop },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                __doPostBack('ctl00$MainContent$refresh', '');
            }
            else {
                jAlert(result.Mess,"Thong bao");
            }
        },
        error: function () {

        }
    });
}

function showAlert(hocvien, lop) {
    jConfirm("Xác nhận xóa?", "Thông báo", function (iOK) {
        if (iOK) {
            XoaHocVienLop(hocvien, lop);
        }
    });
    return false;
}

function XoaHocVienLop(mahocvien, malop) {
    $.ajax({
        type: "POST",
        url: "../Ajax/HocVienAjax.aspx",
        data: { Methods: "XoaHocVienLop", HocVien: mahocvien, Lop: malop },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                __doPostBack('ctl00$MainContent$refresh', '');
            }
            else {
                jAlert(result.Mess, "Thong bao");
            }
        },
        error: function () {

        }
    });
}

function getInfo(obj, MaHV, MaLop) {
    gMaHV = MaHV;
    gMaLop = MaLop;
    $.ajax({
        type: "POST",
        url: "../Ajax/HocVienAjax.aspx",
        data: { Methods: "XemHocVienLop", HocVien: MaHV, Lop: MaLop },
        success: function (result) {
            result = $.parseJSON(result);
            $("#txtSoBuoiNghi").val(result.SoBuoiNghi);
            $("#txtTinhTrang").val(result.TinhTrang);
            var ten = $(obj).attr("relten");
            $("#fCapNhat").dialog({
                title: ten
            });
            $("#fCapNhat").dialog("open");
        },
        error: function () {

        }
    });
    return false;
}

function CapNhatHocVienLop() {
    var BuoiNghi = $("#txtSoBuoiNghi").val();
    var TinhTrang = $("#txtTinhTrang").val();
    $.ajax({
        type: "POST",
        url: "../Ajax/HocVienAjax.aspx",
        data: { Methods: "CapNhatHocVienLop", HocVien: gMaHV, Lop: gMaLop, SoBuoiNghi: BuoiNghi, TinhTrang: TinhTrang },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                __doPostBack('ctl00$MainContent$refresh', '');
                $("#fCapNhat").dialog("close");
            }
            else {
                jAlert(result.Mess, "Thong bao");
            }
        },
        error: function () {

        }
    });
}